home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-bite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-13  |  2.0 KB  |  99 lines

  1. /*  $Id: pl-bite.c,v 1.10 1997/10/13 10:08:32 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: Select part of a file
  8. */
  9.  
  10. #include "pl-incl.h"
  11. #include <stdio.h>
  12. #ifdef HAVE_STRING_H
  13. #include <string.h>
  14. #endif
  15.  
  16. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  17. This is a very simple stand alone program that picks  a  region  from  a
  18. file on character index basis and sends this to the standard output.  It
  19. is  part  of the help system to display long sections of the manual.  It
  20. should be installed as `pl-bite' in a directory that is in the  path  of
  21. the user (normally /usr/local/bin, next to Prolog itself).
  22. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  23.  
  24. static void usage(void);
  25.  
  26. char *program;
  27. int  escape = FALSE;
  28.  
  29. int
  30. main(int argc, char **argv)
  31. { long from, to;
  32.   char *s, *range, *file;
  33.   FILE *fd;
  34.   
  35.  
  36.   program = argv[0];
  37.   argc--, argv++;
  38.  
  39.   if ( argc >= 1 && streq(argv[0], "-e") )
  40.   { escape = TRUE;
  41.     argc--, argv++;
  42.   }
  43.   if ( argc != 2 || (s = strchr(argv[0], ':')) == NULL )
  44.   { usage();
  45.     exit(1);
  46.   }
  47.   range = argv[0];
  48.   file = argv[1];
  49.  
  50.   *s = '\0';
  51.   if ( (from = atol(range)) == 0 || (to = atol(&s[1])) == 0 )
  52.   { usage();
  53.     exit(1);
  54.   }
  55.  
  56.   if ( (fd = fopen(file, "rb")) == NULL || fseek(fd, from, 0) < 0 )
  57.   { perror(argv[2]);
  58.     exit(1);
  59.   }
  60.  
  61.   if ( !escape )
  62.   { int c2 = getc(fd);
  63.  
  64.     while(from < to)
  65.     { int c = c2;
  66.       
  67.       c2 = getc(fd);
  68.       if ( c2 == '\b' )
  69.       {    c2 = getc(fd);
  70.     from += 2;
  71.     continue;
  72.       }
  73.       putchar(c);
  74.       from++;
  75.     }
  76.   } else
  77.   { while(from++ < to)
  78.     { Char c;
  79.   
  80.       if ( (c = getc(fd)) == EOF )
  81.       { fprintf(stderr, "%s: %s: premature EOF\n", program, file);
  82.     exit(1);
  83.       }
  84.       putchar(c);
  85.     }
  86.   }
  87.   fflush(stdout);
  88.   fclose(fd);
  89.  
  90.   exit(0);
  91.   return(0);
  92. }
  93.  
  94. static void
  95. usage(void)
  96. { fprintf(stderr, "usage: %s [-e] from:to file\n", program);
  97.   exit(1);
  98. }
  99.